package com.quanyan.user.service.admin.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.http.HttpClientUtils;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.place.entity.User;
import com.quanyan.user.mapper.TbUserMapper;
import com.quanyan.user.mapper.UserComplainantMapper;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.model.vo.request.admin.ReqAuctionInfoComplainantQuery;
import com.quanyan.user.model.vo.request.admin.ReqDisploseComplainant;
import com.quanyan.user.model.vo.request.admin.ReqReserveComplainantQuery;
import com.quanyan.user.model.vo.request.admin.ReqUserComplainantQuery;
import com.quanyan.user.model.vo.response.admin.RespAuctionInfoComplainant;
import com.quanyan.user.model.vo.response.admin.RespComplainantCategory;
import com.quanyan.user.model.vo.response.admin.RespReserveComplainant;
import com.quanyan.user.model.vo.response.admin.RespUserComplainant;
import com.quanyan.user.service.admin.UserComplainantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hua on 2016/8/24.
 */
@Service
public class UserComplainantServiceImpl implements UserComplainantService {
    private final static Logger logger = LoggerFactory.getLogger(UserComplainantServiceImpl.class);

    @Value("${c.address}")
    private String cAddress;
    @Autowired
    private UserComplainantMapper userComplainantMapper;
    @Autowired
    private TbUserMapper userMapper;

    @Override
    public PageObj<List<RespUserComplainant>> queryUserComplainantList(ReqUserComplainantQuery userComplainantQuery) {
        List<RespUserComplainant> complainants = null;
        int pageNo = userComplainantQuery.getPageNo();
        int total = 0;
        try {
            userComplainantQuery.setPageNo((userComplainantQuery.getPageNo() - 1) * userComplainantQuery.getPageSize());
            if (userComplainantQuery.getStartTime() != null) {
                userComplainantQuery.setStartTime(DateUtils.dayBegin(userComplainantQuery.getStartTime()));
            }
            if (userComplainantQuery.getEndTime() != null) {
                userComplainantQuery.setEndTime(DateUtils.dayEnd(userComplainantQuery.getEndTime()));
            }
            complainants = userComplainantMapper.queryUserComplainantList(userComplainantQuery);
            for (RespUserComplainant complainant : complainants){
                TbUser user = userMapper.selectByPrimaryKey(complainant.getComplainantUid());
                if (user == null) {
                    continue;
                }
                complainant.setComplainantLarkCode(user.getLarkCode());
            }
            total = userComplainantMapper.selectCountByQuery(userComplainantQuery);
        } catch (Exception e) {
            logger.error("获取用户投诉列表失败！", e);
        }
        return PageObj.create(total, pageNo, userComplainantQuery.getPageSize(), complainants);
    }

    @Override
    public PageObj<List<RespReserveComplainant>> queryReserveComplainantList(ReqReserveComplainantQuery reserveComplainantQuery) {
        List<RespReserveComplainant> reserveComplainants = null;
        int pageNo = reserveComplainantQuery.getPageNo();
        int total = 0;
        try {
            reserveComplainantQuery.setPageNo((reserveComplainantQuery.getPageNo()) * reserveComplainantQuery.getPageSize());
            if (reserveComplainantQuery.getStartTime() != null) {
                reserveComplainantQuery.setStartTime(DateUtils.dayBegin(reserveComplainantQuery.getStartTime()));
            }
            if (reserveComplainantQuery.getEndTime() != null) {
                reserveComplainantQuery.setEndTime(DateUtils.dayEnd(reserveComplainantQuery.getEndTime()));
            }
            reserveComplainants = userComplainantMapper.queryReserveComplainantList(reserveComplainantQuery);
            for (RespReserveComplainant complainant : reserveComplainants){
                TbUser user = userMapper.selectByPrimaryKey(complainant.getComplainantUid());
                if (user == null) {
                    continue;
                }
                complainant.setComplainantLarkCode(user.getLarkCode());
            }
            total = userComplainantMapper.reserveComplainantCount(reserveComplainantQuery);
        } catch (Exception e) {
            logger.error("获取一口价投诉列表失败！", e);
        }
        return PageObj.create(total, pageNo, reserveComplainantQuery.getPageSize(), reserveComplainants);
    }

    @Override
    public PageObj<List<RespAuctionInfoComplainant>> queryAuctionInfoComplainantList(ReqAuctionInfoComplainantQuery auctionInfoComplainantQuery) {
        List<RespAuctionInfoComplainant> auctionInfoComplainants = null;
        int pageNo = auctionInfoComplainantQuery.getPageNo();
        int total = 0;
        try {
            auctionInfoComplainantQuery.setPageNo((auctionInfoComplainantQuery.getPageNo()) * auctionInfoComplainantQuery.getPageSize());
            if (auctionInfoComplainantQuery.getStartTime() != null) {
                auctionInfoComplainantQuery.setStartTime(DateUtils.dayBegin(auctionInfoComplainantQuery.getStartTime()));
            }
            if (auctionInfoComplainantQuery.getEndTime() != null) {
                auctionInfoComplainantQuery.setEndTime(DateUtils.dayEnd(auctionInfoComplainantQuery.getEndTime()));
            }
            auctionInfoComplainants = userComplainantMapper.queryAuctionInfoComplainantList(auctionInfoComplainantQuery);
            for (RespAuctionInfoComplainant complainant : auctionInfoComplainants){
                TbUser user = userMapper.selectByPrimaryKey(complainant.getComplainantUid());
                if (user == null) {
                    continue;
                }
                complainant.setComplainantLarkCode(user.getLarkCode());
            }
            total = userComplainantMapper.auctionInfoComplainantCount(auctionInfoComplainantQuery);
        } catch (Exception e) {
            logger.error("获取用户投诉列表失败！", e);
        }
        return PageObj.create(total, pageNo, auctionInfoComplainantQuery.getPageSize(), auctionInfoComplainants);
    }

    @Override
    public List<RespComplainantCategory> queryComplainantCategoryList(Integer complainantUid) {
        List<RespComplainantCategory> complainantCategories = null;
        try {
            complainantCategories = userComplainantMapper.queryComplainantCategoryList(complainantUid);
        } catch (Exception e) {
            logger.error("获取用户投诉列表失败！", e);
        }
        return complainantCategories;
    }

    @Override
    public void updateUserStatus(ReqDisploseComplainant disploseComplainant) {
        Map paramMap = new HashMap();
        paramMap.put("ver","1.0");
        paramMap.put("protocol_ver",1);
        paramMap.put("administratorid", disploseComplainant.getDisposeUid());
        paramMap.put("targetid", disploseComplainant.getComplainedUid());
        paramMap.put("type", disploseComplainant.getUserStatus().intValue());
        String json = JSON.toJSONString(paramMap);
        try {
            String result = HttpClientUtils.postJson(cAddress + "/userInfo/updateUserStatus.api", json, 1000, 1000);
            JSONObject obj = JSON.parseObject(result);
            if (200 == (Integer) obj.get("errcode")) {
                userComplainantMapper.updateUserComplainant(disploseComplainant.getId(), disploseComplainant.getUserStatus());
            }
        } catch (Exception e) {
            logger.error("更新用户状态失败！", e);
        }
    }
}
